package com.uzhie.web.service.report;

import com.uzhie.web.dao.CatalogDAO;
import com.uzhie.web.dao.GpsDAO;
import com.uzhie.web.dao.JT_8702DAO;
import com.uzhie.web.dao.VehicleDAO;
import com.uzhie.web.dao.report.OffLineDAO;
import com.uzhie.exception.VehicleException;
import com.uzhie.web.module.Catalog;
import com.uzhie.web.module.GpsRecord;
import com.uzhie.web.module.ReportOffLine;
import com.uzhie.web.module.Vehicle;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 离线时长统计
 */
@Service
public class OffLineReportService implements ReportHandler {


    @Autowired
    OffLineDAO offLineDAO;
    @Autowired
    JT_8702DAO jt_8702DAO;
    @Autowired
    GpsDAO gpsDAO;
    @Autowired
    VehicleDAO vehicleDAO;
    @Autowired
    CatalogDAO catalogDAO;


    /**
     * 通过车牌号查询车辆最终离线时长
     * @param vehicleNumber 车牌号
     * @param vehicleDepartName 车辆所属部门名称
     * @param userDepartId 用户车辆管理范围
     * @return
     */
    public ReportOffLine queryReportByVehicleNumber(@NotEmpty(message = VehicleException.Status.VEHICLE_ISNULL) String vehicleNumber, String vehicleDepartName, List<String> userDepartId){
        ReportOffLine offLine = null;
        Vehicle vehicle = vehicleDAO.selectVehicleNumber(vehicleNumber);
        if (vehicle!=null && userDepartId.contains(vehicle.getDepartId())) {
            GpsRecord record = gpsDAO.queryListBySIM(vehicle.getSimNumber());
            if (record != null) {
                offLine = ReportOffLine.transformGpsRecord(record, vehicle);
            }
        }
        return offLine;
    }

    public ReportOffLine queryReportByVehicleNumber(@NotEmpty(message = VehicleException.Status.VEHICLE_ISNULL) String vehicleNumber, String vehicleDepartName, String userId){
        ReportOffLine offLine = null;
        List<Catalog> catalogs = catalogDAO.selectCatalogByUserId(userId);
        List<String> userDepartId = new ArrayList<>();
        for (Catalog catalog:catalogs){
            userDepartId.add(catalog.getId());
        }
        Vehicle vehicle = vehicleDAO.selectVehicleNumber(vehicleNumber);
        if (vehicle!=null && userDepartId.contains(vehicle.getDepartId())) {
            GpsRecord record = gpsDAO.queryListBySIM(vehicle.getSimNumber());
            if (record != null) {
                offLine = ReportOffLine.transformGpsRecord(record, vehicle);
            }
        }
        return offLine;
    }

    @Override
    public void handlerTask() {

    }
}
